<template>
  <div>
    <BasicModal width="80%"
                v-bind="$attrs"
                @register="registerModal"
                :title="getTitle"
                @ok="handleSubmit">
      <BasicForm @register="registerForm" />
    </BasicModal>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref, computed, unref, h } from 'vue';
import { BasicForm, useForm } from '/@/components/Form/index';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { DetailsApi, UpdateApi, AddApi } from '/@/api/setCenter/modulemanagement';
import { UploadApi } from '/@/api/article/index';
import { useComponentRegister } from '/@/components/Form';
import { listToTree } from '/@/utils/helper/treeHelper';
import { FormSchema } from '/@/components/Table';
import { Tinymce } from '/@/components/Tinymce/index';
import { getAppEnvConfig } from '/@/utils/env';
useComponentRegister('Tinymce', Tinymce);
export const formSchema: FormSchema[] = [
  {
    label: 'id',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    label: '模块名称',
    field: 'serviceName',
    component: 'Input',
    show: false,
  },
  {
    field: 'picUrl',
    label: 'icon图标',
    component: 'Upload',
    componentProps: {
      api: UploadApi,
      maxNumber: 1,
      accept: ['jpg', 'png', 'JPG', 'PNG'],
      multiple: false,
      maxSize: 5,
    },
    // rules: [
    //   {
    //     required: true,
    //     message: '请上传banner图片文件',
    //   },
    // ],
    colProps: {
      span: 16,
    },
  },
];
export default defineComponent({
  name: 'MenuModal',
  components: { BasicModal, BasicForm },
  emits: ['success', 'register'],
  setup(_, { emit }) {
    const isUpdate = ref(true);
    const areaList = ref<string[]>([]);
    const schoolList = ref<string[]>([]);
    const [
      registerForm,
      { appendSchemaByField, resetFields, setFieldsValue, updateSchema, validate },
    ] = useForm({
      labelWidth: 160,
      schemas: formSchema,
      showActionButtonGroup: false,
      baseColProps: { lg: 12, md: 24 },
    });

    const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
      console.log(data, '编辑内容');

      resetFields();
      isUpdate.value = !!data?.isUpdate;

      if (data.record.id) {
        const imgList = [data.record.picUrl];
        console.log(imgList, 'picUrl');
        setFieldsValue({
          ...data.record,
          picUrl: imgList,
        });
        // updateSchema({
        //   field: 'parentId',
        //   componentProps: {
        //     treeData,
        //   },
        // });
      }
    });

    async function queryList(ids) {
      const data = await DetailsApi(ids);
      if (data.data) {
        // console.log(data.data);

        return data.data;
      }
      return [];
    }
    function add(index) {
      if (index == 1) {
        areaList.value.push('');
      } else if (index == 2) {
        schoolList.value.push('');
      }
    }

    function getDelete(index, a) {
      // 删除
      if (a == 1) {
        areaList.value.splice(index, 1);
      } else if (a == 2) {
        schoolList.value.splice(index, 1);
      }
    }
    const getTitle = computed(() => (!unref(isUpdate) ? '新增城市' : '编辑图标'));

    async function handleSubmit() {
      try {
        setModalProps({ confirmLoading: true });
        const values = await validate();
        // console.log(values.imgUrl[0]);
        values.picUrl = values.picUrl[0];
        console.log(values, '验证');
        await AddApi(values);
        // console.log(values);
        closeModal();
        emit('success');
      } catch (err) {
        console.log(err);
      } finally {
        setModalProps({ confirmLoading: false });
      }
    }

    return {
      registerModal,
      registerForm,
      getTitle,
      areaList,
      schoolList,
      handleSubmit,
      add,
      getDelete,
    };
  },
});
</script>
